#键盘小说网章节批量下载
#
#使用方法：
#将键盘小说网内某部小说的章节列表页面的地址赋值给xiaoshuo_url变量，
#修改file_path说笑文档保存路径
#然后执行脚本即可。
#键盘小说网址：http://www.janpn.info/
#
#以《星辰变》为例，网址：http://www.quanben.co/files/article/html/2/15/index.html
#小说文件保存到当前目录
#
#xuwq
#2024-06-24

import sys
import re
import os
import requests
from lxml import etree
from lxml import html

headers = {  
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'} 

#获取章节内容
def get_content(url_path, content_url):
	url = url_path + content_url
	response = requests.get(url)
	response.encoding= web_charset
	dom_tree = etree.HTML(response.text)
	content = dom_tree.xpath("//*[@id='content']/div[5]/text()")
	return content
	
#获取url路径
def get_url_path(url):
	index = url.rfind("/") + 1
	return url[0:index]

def wirte_load_file(content, encoding, file_name):
    with open(file_name, 'w', encoding=encoding) as f:
        f.write(content.decode(encoding))

def create_html():
     pass

#小说章节列表页地址
xiaoshuo_url = r'http://www.janpn.info/book/zhenjiushiwangguozhijun.html'

#小说内容保存路径
file_path = r'G:\test'
xiaoshuo_name = '朕就是亡国之君'
web_charset = 'utf-8'

file_path = os.path.join(file_path, xiaoshuo_name)

# 判断目录是否存在  
if not os.path.exists(file_path):  
    # 如果目录不存在，则创建它  
    os.makedirs(file_path)  
    print(f"目录 {file_path} 已创建。")  
else:  
    print(f"目录 {file_path} 已存在。")

#content = dict()
#url_path = get_url_path(xiaoshuo_url)

#获取目录页内容
response = requests.get(xiaoshuo_url, headers=headers)
# 检查请求是否成功（状态码为200表示成功）  
if response.status_code == 200:  
    # 使用lxml.html解析HTML内容  
    html_dom = etree.HTML(response.content)
else:  
    print(f"请求失败，状态码为：{response.status_code}")
    exit()

#从目录中获取小说所有章节页面的a标签
chapter_list = html_dom.xpath("/html/body/div[2]/div[3]/ul/li/a")

for chapter_inof in chapter_list:
    chapter_text = str(chapter_inof.xpath('text()')[0])
    chapter_link = chapter_inof.xpath('@href')[0]
    print('获取章节: %s' % chapter_text)

    tmp_response = requests.get(chapter_link, headers=headers) 
    if tmp_response.status_code == 200:  
        # 使用lxml.html解析HTML内容  
        tmp_html_dom = etree.HTML(tmp_response.content)
    else:  
        print('章节获取失败: %s' % chapter_text)
        print(chapter_link)
        exit()

    chapter_title = tmp_html_dom.xpath('/html/body/div/div/div[1]/h1/text()')[0]
    chapter_content = tmp_html_dom.xpath('/html/body/div/div/div[2]/text()')

    #wirte_load_file(chapter_content, web_charset, r'G:\test\朕就是亡国之君\test.html')
    print('标题: %s' % chapter_title)
    print(len(chapter_content))
    exit()